package com.evaluation_system.service.impl;

import com.evaluation_system.exception.checkerNotExistException;
import com.evaluation_system.exception.zeroPageException;
import com.evaluation_system.mapper.CheckerMapper;
import com.evaluation_system.pojo.DTO.CheckerPageQueryDTO;
import com.evaluation_system.pojo.Result.PageResult;
import com.evaluation_system.pojo.entity.Checker;
import com.evaluation_system.pojo.entity.Uploader;
import com.evaluation_system.pojo.entity.saveFileInfo;
import com.evaluation_system.service.CheckerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CheckerServiceImpl implements CheckerService {

    @Autowired
    private CheckerMapper checkermapper;

    @Override
    public List<saveFileInfo> readFileForChecker(Integer size, Integer page, String filename) {
        try{
            if(filename==null || filename.equals(""))
                return checkermapper.getFileForChecker(size,(page-1)*size);
            else
                return checkermapper.getOneFileForChecker(filename);
        } catch (Exception e) {
            throw new zeroPageException("页面为0异常");
        }
    }

    @Override
    public void updateChecker(Integer fileId, Integer checkerId, Integer checked) {
        if(checkermapper.checkerIsTable(checkerId)==0)
            throw new checkerNotExistException("查无此检查用户");

        checkermapper.updateChecker(checked,fileId);
    }

    @Override
    public PageResult<Checker> pageQuery(CheckerPageQueryDTO checkerPageQueryDTO) {
        // 开启分页
        PageHelper.startPage(checkerPageQueryDTO.getPage(), checkerPageQueryDTO.getPageSize());

        // 执行查询
        Page<Checker> page = checkermapper.pageQuery(checkerPageQueryDTO);

        // 封装结果
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Checker checker) {
        checkermapper.add(checker);

    }

    @Override
    public void update(Checker checker) {
        checkermapper.update(checker);
    }

    @Override
    public void deleteById(Integer id) {
        checkermapper.deleteById(id);

    }
}
